﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Windows;
namespace Gene.Render
{
    /// <summary>
    /// 背景平面，一般是纯色，用户做背景.初始为xy平面。可以通过旋转和平移来改变其位置。这样比较方便。
    /// </summary>
    /// <remarks>
    /// 不确定确实有用，不过可能在需要显示x平面，z平面等时候需要。
    /// 也可能在显示坐标轴时候需要。
    /// </remarks>
    public abstract class BackgroudPlane
    {

        public const int BK_IMG_WIDTH = 1024;
        protected const int STANDARD_BK_IMG_HEIGHT = 768;
        protected int BK_IMG_HEIGHT = STANDARD_BK_IMG_HEIGHT;
        public const int ARROW_RADIUS = 15;
        public const int ARROW_LENGTH = 50;
        public const int BRUSH_THICKNESS = 5;
        public const int MARKER_LEN = 5;
        GeometryModel3D _GeometryModel = new GeometryModel3D();

        public GeometryModel3D GeometryModel
        {
            get { return _GeometryModel; }
            set { _GeometryModel = value; }
        }
        MeshGeometry3D _MeshGeometry3D = new MeshGeometry3D();
        public Brush Drawer
        {
            set;
            get;
        }
  
        public RenderTargetBitmap Image
        {
            get;
            set;
        }

        public void AddTo(Model3DGroup modelGroup)
        {
            //Build(flag);
            modelGroup.Children.Add(_GeometryModel);
        }

        protected abstract Point3DCollection BuildPositions();
        
        public void Build()
        {
            // Create a collection of normal vectors for the MeshGeometry3D.
            //Vector3DCollection myNormalCollection = new Vector3DCollection();
            //myNormalCollection.Add(new Vector3D(0, 0, 1));
            //myNormalCollection.Add(new Vector3D(0, 0, 1));
            //myNormalCollection.Add(new Vector3D(0, 0, 1));
            //myNormalCollection.Add(new Vector3D(0, 0, 1));
            //_MeshGeometry3D.Normals = myNormalCollection;

            // Create a collection of vertex positions for the MeshGeometry3D. 
            Point3DCollection myPositionCollection = BuildPositions();
            _MeshGeometry3D.Positions = myPositionCollection;



            // Create a collection of triangle indices for the MeshGeometry3D.
            Int32Collection myTriangleIndicesCollection = new Int32Collection();
            myTriangleIndicesCollection.Add(0);
            myTriangleIndicesCollection.Add(1);
            myTriangleIndicesCollection.Add(2);

            myTriangleIndicesCollection.Add(2);
            myTriangleIndicesCollection.Add(3);
            myTriangleIndicesCollection.Add(0);
           
            myTriangleIndicesCollection.Add(0);
            myTriangleIndicesCollection.Add(3);
            myTriangleIndicesCollection.Add(2);

            myTriangleIndicesCollection.Add(2);
            myTriangleIndicesCollection.Add(1);
            myTriangleIndicesCollection.Add(0);
            _MeshGeometry3D.TriangleIndices = myTriangleIndicesCollection;

            // Apply the mesh to the geometry model.
            _GeometryModel.Geometry = _MeshGeometry3D;

            // Create a collection of texture coordinates for the MeshGeometry3D.
            PointCollection myTextureCoordinatesCollection = new PointCollection();
            myTextureCoordinatesCollection.Add(new Point(0, 0));
            myTextureCoordinatesCollection.Add(new Point(0, 1));
            myTextureCoordinatesCollection.Add(new Point(1, 1));
            myTextureCoordinatesCollection.Add(new Point(1, 0));
            
            _MeshGeometry3D.TextureCoordinates = myTextureCoordinatesCollection;

            BuildBackgroundDrawer();
            // Define material and apply to the mesh geometries.
            EmissiveMaterial myMaterial = new EmissiveMaterial(Drawer);
            _GeometryModel.Material = myMaterial;

        }
        protected abstract void BuildBackgroundDrawer();
        

    }
}
